# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName : zsj.py
# @Time     : 2024/4/21 17:12
# @Author   : Robot-Zsj
"""
description:

i:
2
0 3
3
0 0 2
1 3 4
6 6 4

o:
0 1 1 1
"""

n = int(input())  # 明文数字个数
datas = list(map(int, input().split()))  # 明文

m = int(input())  # 密码本矩阵大小
secrets = []  # 密码本

starts = []

for i in range(m):
    secrets.append(list(map(int, input().split())))
    for j in range(m):
        if secrets[i][j] == datas[0]:
            starts.append((i, j))

offsets = ((-1, 0), (0, -1), (1, 0), (0, 1))


def dfs(x, y, index, path, used):
    if index == n:
        return True

    for offsetX, offsetY in offsets:
        newX = x + offsetX
        newY = y + offsetY

        if newX < 0 or newX >= m or newY < 0 or newY >= m or used[newX][newY] or secrets[newX][newY] != datas[index]:
            continue

        path.append(f"{newX} {newY}")
        used[newX][newY] = True

        if dfs(newX, newY, index + 1, path, used):
            return True

        used[newX][newY] = False
        path.pop()
    return False


def getResult():
    for x, y in starts:
        used = [[False] * m for _ in range(m)]
        used[x][y] = True

        path = [f'{x} {y}']

        if dfs(x, y, 1, path, used):
            return " ".join(path)
    return "error"


print(getResult())
